package com.eigaland.change_machine.business.selectseat

import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.alexvasilkov.gestures.GestureController
import com.eigaland.change_machine.R
import com.eigaland.change_machine.business.movielist.MovieListItemVM
import com.eigaland.change_machine.business.selectseat.buyseat.BuySeatFragment
import com.eigaland.change_machine.business.show.ShowListItemVM
import com.eigaland.change_machine.customview.SeatPlanView
import com.eigaland.change_machine.customview.SeatViewFake
import com.eigaland.change_machine.databinding.FragmentSelectSeatBinding
import com.eigaland.change_machine.displaymodel.ShowDisplayModel
import com.eigland.eigaland_lib.base.BaseLazyFragment
import com.eigland.eigaland_lib.model.SeatPlan
import kotlinx.android.synthetic.main.fragment_select_seat.*
import movie6.posprintercept2.extensions.removeParentView

class SelectSeatFragment :BaseLazyFragment<SelectSeatFragmentVM,FragmentSelectSeatBinding>(){
    override fun initVM() {

        var show = arguments?.getSerializable("show") as ShowDisplayModel
        var movie = arguments?.getSerializable("movie") as MovieListItemVM

        var fragemnt = BuySeatFragment()
        var bd = Bundle()
        bd.putSerializable("movie",movie)
        bd.putSerializable("show",show)
        fragemnt.arguments = bd

        supportDelegate.loadRootFragment(R.id.buy_ticket_frame,fragemnt)

        vm?.loadSeat(show.showId)


        vm?.displaySeat?.observe(this, Observer {
           /* var seat = SeatViewFake(requireContext(),null)
            seat.setSeatPlanData(it)*/

            seat_frame.addView(SeatPlanView(requireContext(),it).removeParentView())
        })


        seat_frame.controller?.setOnGesturesListener(object :GestureController.OnGestureListener{
            override fun onSingleTapUp(event: MotionEvent): Boolean {

                val gflState = seat_frame.controller.state.copy()
                gflState.zoomTo(2f, event.x, event.y)
                seat_frame.controller.animateStateTo(gflState)

                return false
            }

            override fun onDown(event: MotionEvent) {

            }

            override fun onDoubleTap(event: MotionEvent): Boolean {

                return false
            }

            override fun onUpOrCancel(event: MotionEvent) {
            }

            override fun onLongPress(event: MotionEvent) {

            }

            override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
                return false
            }

        })
    }

    override fun setUpViewData(view: View) {
        binding = DataBindingUtil.bind(view)
        vm = SelectSeatFragmentVM()
        binding?.let {
            it.lifecycleOwner = this
            it.selectSeatFragmentVM = vm
        }


    }

    override fun setLayoutId() = R.layout.fragment_select_seat

}